<template>
  <div class="add-container">
    <el-form
      :model="form"
      ref="ruleForm"
      :rules="rules"
      label-width="120px"
      class="add-form"
    >
      <el-card class="add-card">
        <el-form-item
          label="上级分类"
          label-width="80px"
          icon="el-icon-phone"
          prop="parentId"
        >
          <!-- select -->
          <el-select v-model="form.parentId" placeholder="请选择">
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类编码" label-width="80px" prop="code">
          <el-input v-model="form.code" placeholder="请输入分类编码" />
        </el-form-item>
        <el-form-item label="分类名称" label-width="80px" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="说明" label-width="80px" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入说明" />
        </el-form-item>
        <el-form-item label="状态" label-width="80px" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button type="warning" @click="goBack()">返回</el-button>
        </el-form-item>
      </el-card>
    </el-form>
  </div>
</template>

<script>
import { assetCategory } from "@/api/assetCategory.js";
export default {
  data() {
    return {
      form: {
        parentId: "",
        code: "",
        name: "",
        remark: "",
        status: "",
      },
      rules: {
        // parentId: [
        //   { required: true, message: "请选择上级分类", trigger: "change" },
        // ],
        code: [{ required: true, message: "请输入分类编码", trigger: "blur" }],
        name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
        // remark: [{ required: true, message: "请输入说明", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
      categoryList: [],
      size: "medium",
    };
  },
  created() {
    this.getAssetCategoryList();
  },
  methods: {
    getAssetCategoryList() {
      assetCategory.findAll().then((res) => {
        this.categoryList = res.data;
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // console.log("submit!");
          // 提交逻辑，例如发送到后端
          assetCategory
            .add(this.form)
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.$router.push({
                name: "AssetCategory",
              });
            })
            .catch(() => {
              this.$message({
                message: res.msg,
                type: "danger",
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<style scoped></style>
